package com.lgj.tlias.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Component
@Slf4j
@Aspect
public class RecordTimeAspect {
    //环绕通知

    //切入点表达式
    // (1)execution 根据方法签名
    //（2)@annotation 根据注解匹配
//    @Before("@annotation(com.lgj.tlias.annotation.RecordTime)")
    @Around("execution(* com.lgj.tlias.service.impl.DeptServiceImpl.*(..))")
    public Object recordTime(ProceedingJoinPoint pjp) throws Throwable {
        // 1.记录开始时间
        long start = System.currentTimeMillis();
        // 2.执行目标方法
        Object ret = pjp.proceed();
        // 3.记录结束时间
        long end = System.currentTimeMillis();
        // 4.日志记录
        log.info("{}执行耗时:{}ms",pjp.getSignature(),end-start);
        return ret;
    }
//    @Before 环绕通知
//    @After 后置通知
//    @AfterReturning 返回后通知
//    @AfterThrowing 异常通知
}
